using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using shuju_jiange.Com;

namespace shuju_jiange
{
    public partial class FrmDeletedDataDisplay : Form
    {
        private List<Data> deletedData;
        
        public FrmDeletedDataDisplay(List<Data> deletedDataList)
        {
            InitializeComponent();
            this.deletedData = deletedDataList;
            LoadDeletedData();
        }
        
        private void LoadDeletedData()
        {
            if (deletedData == null || deletedData.Count == 0)
            {
                MessageBox.Show("没有删除的数据", "提示");
                return;
            }
            
            // 创建DataTable来绑定到DataGridView，与主界面列保持一致
            DataTable dt = new DataTable();
            dt.Columns.Add("ID", typeof(int));
            dt.Columns.Add("%总和", typeof(double));
            dt.Columns.Add("数据", typeof(string));
            dt.Columns.Add("出现次数", typeof(int));
            dt.Columns.Add("理论间隔", typeof(float));
            dt.Columns.Add("次数平均间隔", typeof(int));
            dt.Columns.Add("Z值", typeof(float));
            dt.Columns.Add("数据平均间隔", typeof(int));
            dt.Columns.Add("数据末端差", typeof(int));
            dt.Columns.Add("新数据间隔Y", typeof(float));
            dt.Columns.Add("新末端差X", typeof(float));
            dt.Columns.Add("偏移数据", typeof(string));
            dt.Columns.Add("值相加", typeof(int));
            dt.Columns.Add("值相加%", typeof(float));
            dt.Columns.Add("次数差", typeof(int));
            dt.Columns.Add("差值", typeof(float));
            dt.Columns.Add("列X", typeof(float));
            dt.Columns.Add("占有率", typeof(double));
            dt.Columns.Add("A", typeof(int));
            dt.Columns.Add("A%", typeof(float));
            dt.Columns.Add("H", typeof(int));
            dt.Columns.Add("H%", typeof(float));
            dt.Columns.Add("B", typeof(int));
            dt.Columns.Add("B%", typeof(float));
            dt.Columns.Add("AAC2", typeof(int));
            dt.Columns.Add("AAC2%", typeof(float));
            dt.Columns.Add("ACH2", typeof(int));
            dt.Columns.Add("ACH2%", typeof(float));
            dt.Columns.Add("ABG2", typeof(int));
            dt.Columns.Add("ABG2%", typeof(float));
            dt.Columns.Add("BAC2", typeof(int));
            dt.Columns.Add("BAC2%", typeof(float));
            dt.Columns.Add("BGH2", typeof(int));
            dt.Columns.Add("BGH2%", typeof(float));
            dt.Columns.Add("BBG2", typeof(int));
            dt.Columns.Add("BBG2%", typeof(float));
            dt.Columns.Add("A1", typeof(int));
            dt.Columns.Add("A1%", typeof(float));
            dt.Columns.Add("A2", typeof(int));
            dt.Columns.Add("A2%", typeof(float));
            dt.Columns.Add("A3", typeof(int));
            dt.Columns.Add("A3%", typeof(float));
            dt.Columns.Add("A4", typeof(int));
            dt.Columns.Add("A4%", typeof(float));
            dt.Columns.Add("A5", typeof(int));
            dt.Columns.Add("A5%", typeof(float));
            dt.Columns.Add("A6", typeof(int));
            dt.Columns.Add("A6%", typeof(float));
            dt.Columns.Add("B1", typeof(int));
            dt.Columns.Add("B1%", typeof(float));
            dt.Columns.Add("B2", typeof(int));
            dt.Columns.Add("B2%", typeof(float));
            dt.Columns.Add("B3", typeof(int));
            dt.Columns.Add("B3%", typeof(float));
            dt.Columns.Add("B4", typeof(int));
            dt.Columns.Add("B4%", typeof(float));
            dt.Columns.Add("B5", typeof(int));
            dt.Columns.Add("B5%", typeof(float));
            dt.Columns.Add("B6", typeof(int));
            dt.Columns.Add("B6%", typeof(float));
            dt.Columns.Add("异常值标记", typeof(string));
            
            foreach (Data data in deletedData)
            {
                DataRow row = dt.NewRow();
                row["ID"] = data.Id;
                row["%总和"] = Math.Round(data.Total, 4);
                row["数据"] = data.SrcData;
                row["出现次数"] = data.Time;
                row["理论间隔"] = Math.Round(data.TheoreticalInterval, 4);
                row["次数平均间隔"] = data.Blue;
                row["Z值"] = Math.Round(data.ZValue, 4);
                row["数据平均间隔"] = data.Interval;
                row["数据末端差"] = data.DataEndDiff;
                row["新数据间隔Y"] = Math.Round(data.NewDataIntervalY, 4);
                row["新末端差X"] = Math.Round(data.NewEndDiffX, 4);
                row["偏移数据"] = data.DeviationString;
                row["值相加"] = data.Sum;
                row["值相加%"] = Math.Round(data.Sum_percent, 4);
                row["次数差"] = data.Cha;
                row["差值"] = Math.Round(data.DiffValue, 4);
                row["列X"] = Math.Round(data.ColumnX, 4);
                row["占有率"] = Math.Round(data.Zhanyoulv, 4);
                row["A"] = data.Ac;
                row["A%"] = Math.Round(data.Ac_percent, 4);
                row["H"] = data.H;
                row["H%"] = Math.Round(data.H_percent, 4);
                row["B"] = data.Bg;
                row["B%"] = Math.Round(data.Bg_percent, 4);
                row["AAC2"] = data.AAc2;
                row["AAC2%"] = Math.Round(data.AAc2_percent, 4);
                row["ACH2"] = data.Ach2;
                row["ACH2%"] = Math.Round(data.Ach2_percent, 4);
                row["ABG2"] = data.ABg2;
                row["ABG2%"] = Math.Round(data.ABg2_percent, 4);
                row["BAC2"] = data.BAc2;
                row["BAC2%"] = Math.Round(data.BAc2_percent, 4);
                row["BGH2"] = data.Bgh2;
                row["BGH2%"] = Math.Round(data.Bgh2_percent, 4);
                row["BBG2"] = data.BBg2;
                row["BBG2%"] = Math.Round(data.BBg2_percent, 4);
                row["A1"] = data.A1;
                row["A1%"] = Math.Round(data.A1_percent, 4);
                row["A2"] = data.A2;
                row["A2%"] = Math.Round(data.A2_percent, 4);
                row["A3"] = data.A3;
                row["A3%"] = Math.Round(data.A3_percent, 4);
                row["A4"] = data.A4;
                row["A4%"] = Math.Round(data.A4_percent, 4);
                row["A5"] = data.A5;
                row["A5%"] = Math.Round(data.A5_percent, 4);
                row["A6"] = data.A6;
                row["A6%"] = Math.Round(data.A6_percent, 4);
                row["B1"] = data.B1;
                row["B1%"] = Math.Round(data.B1_percent, 4);
                row["B2"] = data.B2;
                row["B2%"] = Math.Round(data.B2_percent, 4);
                row["B3"] = data.B3;
                row["B3%"] = Math.Round(data.B3_percent, 4);
                row["B4"] = data.B4;
                row["B4%"] = Math.Round(data.B4_percent, 4);
                row["B5"] = data.B5;
                row["B5%"] = Math.Round(data.B5_percent, 4);
                row["B6"] = data.B6;
                row["B6%"] = Math.Round(data.B6_percent, 4);
                row["异常值标记"] = data.IsAnomalyValue ? "是" : "否";
                dt.Rows.Add(row);
            }
            
            dataGridViewDeleted.DataSource = dt;
            
            // 设置列宽
            dataGridViewDeleted.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            
            // 更新标题
            this.Text = $"已删除的异常数据 - 共{deletedData.Count}条";
            labelInfo.Text = $"共删除了 {deletedData.Count} 条异常数据";
        }
        
        private void btnExportDeleted_Click(object sender, EventArgs e)
        {
            if (deletedData == null || deletedData.Count == 0)
            {
                MessageBox.Show("没有可导出的数据", "提示");
                return;
            }
            
            SaveFileDialog saveDialog = new SaveFileDialog();
            saveDialog.Filter = "CSV文件|*.csv|文本文件|*.txt";
            saveDialog.Title = "导出删除的数据";
            saveDialog.FileName = $"删除的异常数据_{DateTime.Now:yyyyMMdd_HHmmss}";
            
            if (saveDialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    using (System.IO.StreamWriter writer = new System.IO.StreamWriter(saveDialog.FileName, false, Encoding.UTF8))
                    {
                        // 写入表头，与主界面列保持一致
                        writer.WriteLine("ID,%总和,数据,出现次数,理论间隔,次数平均间隔,Z值,数据平均间隔,数据末端差,新数据间隔Y,新末端差X,偏移数据,值相加,值相加%,次数差,差值,列X,占有率,A,A%,H,H%,B,B%,AAC2,AAC2%,ACH2,ACH2%,ABG2,ABG2%,BAC2,BAC2%,BGH2,BGH2%,BBG2,BBG2%,A1,A1%,A2,A2%,A3,A3%,A4,A4%,A5,A5%,A6,A6%,B1,B1%,B2,B2%,B3,B3%,B4,B4%,B5,B5%,B6,B6%,异常值标记");
                        
                        // 写入数据
                        foreach (Data data in deletedData)
                        {
                            writer.WriteLine($"{data.Id},{data.Total:F4},{data.SrcData},{data.Time},{data.TheoreticalInterval:F4},{data.Blue},{data.ZValue:F4},{data.Interval},{data.DataEndDiff},{data.NewDataIntervalY:F4},{data.NewEndDiffX:F4},{data.DeviationString},{data.Sum},{data.Sum_percent:F4},{data.Cha},{data.DiffValue:F4},{data.ColumnX:F4},{data.Zhanyoulv:F4},{data.Ac},{data.Ac_percent:F4},{data.H},{data.H_percent:F4},{data.Bg},{data.Bg_percent:F4},{data.AAc2},{data.AAc2_percent:F4},{data.Ach2},{data.Ach2_percent:F4},{data.ABg2},{data.ABg2_percent:F4},{data.BAc2},{data.BAc2_percent:F4},{data.Bgh2},{data.Bgh2_percent:F4},{data.BBg2},{data.BBg2_percent:F4},{data.A1},{data.A1_percent:F4},{data.A2},{data.A2_percent:F4},{data.A3},{data.A3_percent:F4},{data.A4},{data.A4_percent:F4},{data.A5},{data.A5_percent:F4},{data.A6},{data.A6_percent:F4},{data.B1},{data.B1_percent:F4},{data.B2},{data.B2_percent:F4},{data.B3},{data.B3_percent:F4},{data.B4},{data.B4_percent:F4},{data.B5},{data.B5_percent:F4},{data.B6},{data.B6_percent:F4},{(data.IsAnomalyValue ? "是" : "否")}");
                        }
                    }
                    
                    MessageBox.Show("导出成功！", "提示");
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"导出失败：{ex.Message}", "错误");
                }
            }
        }
        
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}